package com.bianguo.android.baidumapdemo.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bianguo.android.baidumapdemo.R;
import com.bianguo.android.baidumapdemo.utils.MLog;

import java.util.Random;

/**
 * Created by Administrator on 2018/6/7.
 */

public class HeartView extends ViewGroup {

    private int mWidth;
    private int mHeight;
    private Bitmap heartBitmap;
    private Paint mPaint;
    private PointF mStartPointF, mEndPointF, mControlPointOne, mControlPointTwo;
    private Random random;
    private Context mContext;
    private int[] colors = {Color.WHITE, Color.CYAN, Color.YELLOW, Color.BLACK, Color.LTGRAY, Color.GREEN, Color.RED};

    public HeartView(Context context) {
        super(context);
        init(context);
    }

    public HeartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HeartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context mContext) {
        this.mContext = mContext;
        heartBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.red_heart);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(10);
        mStartPointF = new PointF();
        mEndPointF = new PointF();
        mControlPointOne = new PointF();
        mControlPointTwo = new PointF();
        random = new Random();

        ImageView image_heard = new ImageView(mContext);
        image_heard.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        image_heard.setImageBitmap(heartBitmap);
        image_heard.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                startBezierAnim();
            }
        });
        addView(image_heard);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            int childW = child.getMeasuredWidth();
            int childH = child.getMeasuredHeight();
            child.layout((mWidth - childW) / 2, (mHeight - childH), (mWidth - childW) / 2 + childW, mHeight);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //获取view的宽高测量模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        //保存测量高度
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;

        //借用第一个子view控件中的宽高
        View child = getChildAt(0);
        int childW = child.getMeasuredWidth();
        int childH = child.getMeasuredHeight();

        mStartPointF.x = (mWidth - childW) / 2;
        mStartPointF.y = mHeight - childH;
        mEndPointF.x = (mWidth - childW) / 2;
        mEndPointF.y = 0 - childH;

        mControlPointOne.x = random.nextInt(mWidth / 2);
        mControlPointOne.y = random.nextInt(mHeight / 2) + mHeight / 2;

        mControlPointTwo.x = random.nextInt(mWidth / 2) + mWidth / 2;
        mControlPointTwo.y = random.nextInt(mHeight / 2);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    private Bitmap drawableHeart(int color) {
        Bitmap newBitmap = Bitmap.createBitmap(heartBitmap.getWidth(), heartBitmap.getWidth(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(heartBitmap, 0, 0, mPaint);
        canvas.drawColor(color, PorterDuff.Mode.SRC_ATOP);
        canvas.setBitmap(null);
        return newBitmap;
    }

    public void startBezierAnim() {
        final ImageView imageView = new ImageView(mContext);
        imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        imageView.setImageBitmap(drawableHeart(colors[random.nextInt(colors.length)]));
        addView(imageView);
        invalidate();
        //开始做动画效果
        PointF endPointRandom = new PointF(random.nextInt(mWidth), mEndPointF.y);
        BezierTypeEvaluator bezierTypeEvaluator = new BezierTypeEvaluator(new PointF(random.nextInt(mWidth), random.nextInt(mHeight)), new PointF(random.nextInt(mWidth), random.nextInt(mHeight)));
        ValueAnimator valueAnimator = ValueAnimator.ofObject(bezierTypeEvaluator, mStartPointF, endPointRandom);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
                invalidate();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(imageView);

            }
        });

        valueAnimator.setDuration(1000);
        valueAnimator.start();
    }

    public class BezierTypeEvaluator implements TypeEvaluator<PointF> {

        private PointF controlPoint1, controlPoint2;

        public BezierTypeEvaluator(PointF controlPoint1, PointF controlPoint2) {
            this.controlPoint1 = controlPoint1;
            this.controlPoint2 = controlPoint2;
        }

        @Override
        public PointF evaluate(float fraction, PointF pointF, PointF endValue) {
            PointF pointCur = new PointF();
            pointCur.x = mStartPointF.x * (1 - fraction) * (1 - fraction) * (1 - fraction) + 3
                    * controlPoint1.x * fraction * (1 - fraction) * (1 - fraction) + 3
                    * controlPoint2.x * (1 - fraction) * fraction * fraction + endValue.x * fraction * fraction * fraction;// 实时计算最新的点X坐标
            pointCur.y = mStartPointF.y * (1 - fraction) * (1 - fraction) * (1 - fraction) + 3
                    * controlPoint1.y * fraction * (1 - fraction) * (1 - fraction) + 3
                    * controlPoint2.y * (1 - fraction) * fraction * fraction + endValue.y * fraction * fraction * fraction;// 实时计算最新的点Y坐标
            return pointCur;
        }
    }
}
